package com.cuanbo.dualscreenteachzhu.utils;

import android.view.View;
import android.view.ViewGroup;

/**
 * 启用或禁用Activity中所有子View的点击事件工具类
 */
public class ActivityViewUtil {
    private View[] aryView;
    private View rtView;

    public ActivityViewUtil(View rtView) {
        this.rtView = rtView;
    }

    /**
     * 禁用所有View
     *
     * @param aryView 视图数组
     */
    public void disable(View[] aryView) {
        if (rtView == null) return;
        this.aryView = aryView;
        disableRecursive(rtView);
    }

    /**
     * 禁用所有View
     *
     * @param view 视图
     */
    private void disableRecursive(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View v = vp.getChildAt(i);
                boolean bl = false;
                for (int j = 0; j < aryView.length; j++) {
                    if (aryView[j] == null) continue;
                    if (v.getId() == aryView[j].getId()) {
                        bl = true;
                        break;
                    }
                }
                if (!bl) {
                    v.setEnabled(false);
                }
                disableRecursive(v);
            }
        }
    }

    /**
     * 启用所有View
     */
    public void enable() {
        if (rtView == null) return;
        enableRecursive(rtView);
    }

    /**
     * 启用所有View
     *
     * @view 视图
     */
    private void enableRecursive(View view) {
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View v = vp.getChildAt(i);
                try {
                    v.setEnabled(true);
                } catch (Exception e) {

                }
                enableRecursive(v);
            }
        }
    }
}
